<?php

require_once "Str.php";

class QueryBuilder
{
	function smartBuild($myId, $scheme, $tablePrefix='')
	//if editing set changeId otherwise adding
	{		
		$what = "$myId.*";
		$from = $myId;
		
		foreach($scheme as $e)
		{
			if (Str::startsWith($e['Field'], 'id_'))
			//foreign key in some other table
			//so we show select field with id's as values and 
			//what is described in 'Labels' as labels
			//in that table key must be id
			{
				if (isset($e['SelectLabelRow']))
				{
					$otherTableName = substr($e['Field'], 3);
					$otherTable = $tablePrefix.$otherTableName;
					$what .= ", {$otherTable}.{$e['SelectLabelRow']} AS {$otherTableName}_{$e['SelectLabelRow']}";
					$from .= " JOIN $otherTable ON $myId.{$e['Field']} = $otherTable.id ";
				}
			}
		}
		return array($what, $from);			
	}	
}
?>